package com.hfjyz.registration.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.hfjyz.registration.command.RegistCommand;
import com.hfjyz.registration.model.User;
import com.hfjyz.registration.util.CryptUtil;
import com.hfjyz.registration.util.ValidatorUtil;

public class RegistValidator implements Validator {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean supports(Class clazz) {
		// TODO Auto-generated method stub
		return clazz.isAssignableFrom(RegistCommand.class);
	}

	public void validate(Object target, Errors errors) {
		
	}

	public void validateId(Object target, Errors errors,int messageFlag) {
		if (messageFlag == 1) {
			errors.rejectValue("idCardNoError", "required.idCardNoError",
			"招聘岗位未设置，请联系管理员!");
		} else if (messageFlag == 2) {
			errors.rejectValue("idCardNoError", "required.idCardNoError",
			"招聘职位未设置，请联系管理员!");
		}
	}

	public void validateRegistInfo(Object target, Errors errors,
			User idCardNoLst) {
		RegistCommand v = (RegistCommand) target;

		if ("1".equals(v.getEditFlg())) {
			User user = null;
			if (v.getIdCardNo() == null || v.getIdCardNo().length() == 0) {
				errors.rejectValue("idCardNoError", "required.idCardNoError",
						"身份证号必须填写!");
			} else {
				if (!ValidatorUtil.isValidate18Idcard(v.getIdCardNo())) {
					errors.rejectValue("idCardNoError",
							"required.idCardNoError", "身份证号输入不正确!");
				} else {
					if (idCardNoLst == null) {
						errors.rejectValue("idCardNoError",
								"required.idCardNoError", "身份证号不存在!");
					} else {
						user = idCardNoLst;
					}
				}
			}
			if (v.getPassword() == null || v.getPassword().length() == 0) {
				errors.rejectValue("password", "required.password", "密码必须填写!");
			} else {
				if (user != null
						&& !CryptUtil.encrypt(v.getPassword()).equals(
								user.getPassword())) {
					errors.rejectValue("password", "required.password", "密码错误!");
				}
			}
		} else {
			if (v.getName() == null || v.getName().length() == 0) {
				errors.rejectValue("name", "required.name", "用户名必须填写!");
			}
			if (v.getIdCardNo() == null || v.getIdCardNo().length() == 0) {
				errors.rejectValue("idCardNoError", "required.idCardNoError",
						"身份证号必须填写!");
			} else {
				if (!ValidatorUtil.isValidate18Idcard(v.getIdCardNo())) {
					errors.rejectValue("idCardNoError",
							"required.idCardNoError", "身份证号输入不正确!");
				} else {
					if (idCardNoLst != null) {
						errors.rejectValue("idCardNoError",
								"required.idCardNoError", "身份证号已存在!");
					}
				}
			}
			if (v.getPassword() == null || v.getPassword().length() == 0) {
				errors.rejectValue("password", "required.password", "密码必须填写!");
			} else {
				if (!v.getPassword().equals(v.getRePassword())) {
					errors.rejectValue("rePasswordError",
							"required.rePasswordError", "密码与重复密码必须一致!");
				}
			}
		}
	}

	public void validatePeopleInfo(Object target, Errors errors) {
		RegistCommand v = (RegistCommand) target;
		if (v.getHomeTown() == null || v.getHomeTown().length() == 0) {
			errors.rejectValue("homeTown", "required.homeTown", "籍贯必须填写!");
		}
//		if (v.getDegree() == null || v.getDegree().length() == 0) {
//			errors.rejectValue("degree", "required.degree", "最高学历必须填写!");
//		}
		if (v.getUserSchool() == null || v.getUserSchool().length() == 0) {
			errors.rejectValue("userSchool", "required.userSchool", "毕业院校必须填写!");
		}
		if (v.getWorkYears() == null || v.getWorkYears().length() == 0) {
			errors.rejectValue("workYears", "required.workYears", "工作年限必须填写!");
		} else if (!ValidatorUtil.isDigital(v.getWorkYears())) {
			errors.rejectValue("workYears", "required.workYears", "工作年限必须为数字!");
		}
		if (v.getUserMajor() == null || v.getUserMajor().length() == 0) {
			errors.rejectValue("userMajor", "required.userMajor", "专业必须填写!");
		}
//		if (v.getComputerSkill() == null || v.getComputerSkill().length() == 0) {
//			errors.rejectValue("computerSkill", "required.computerSkill",
//					"计算机程度必须填写!");
//		}
//		if (v.getLanguageSkill() == null || v.getLanguageSkill().length() == 0) {
//			errors.rejectValue("languageSkill", "required.languageSkill",
//					"外语程度必须填写!");
//		}
		if (v.getTelephone() == null || v.getTelephone().length() == 0) {
			errors.rejectValue("telephone", "required.telephone", "联系电话必须填写!");
		}
		if (v.getHeight() == null || v.getHeight().length() == 0) {
			errors.rejectValue("height", "required.height", "身高必须填写!");
		} else if (!ValidatorUtil.isDigital(v.getHeight())) {
			errors.rejectValue("height", "required.height", "身高必须为数字!");
		}

		if (v.getTrainingExp() != null && v.getTrainingExp().length() > 512) {
			errors.rejectValue("trainingExp", "required.trainingExp",
					"输入文字过长，请概括描述学习/培训经历至512文字以下!");
		}
		if (v.getWorkExp() != null && v.getWorkExp().length() > 512) {
			errors.rejectValue("workExp", "required.workExp",
					"输入文字过长，请概括描述工作经历至512文字以下!");
		}
		if (v.getSocialRel() != null && v.getSocialRel().length() > 512) {
			errors.rejectValue("socialRel", "required.socialRel",
					"输入文字过长，请概括描述社会关系至512文字以下!");
		}
	}
}
